--
-- Author: shunguo.chen
-- Date: 2019-05-24 14:25:47
--
-- SkillRrender 技能render
--

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.createWithData(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.SkillIcon))
	sprite:init(data)
	return sprite
end

function ClassRef.create(node)
	return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
	self.super.init(self, data)
    display.setCascadeOpacityEnabled(self._ui.Panel,true)
    display.setCascadeColorEnabled(self._ui.Panel,true)
    self._ui.Panel:setOpacity(255)

	self:setContentSize(self._ui.Panel:getContentSize())

    self._idx = 1
    self._canChange = false

	self:setChooseed(false)
	self:setSelected(false)
    self:setClickEnable(true)

	display.uiAddClick(self._ui["Panel/Panel_skill"], function(sender, eventType)
		executeFunc(self._onClick, self)
	end)

    self._ui["Panel/Button"]:addClickEventListener(function()
        executeFunc(self._onChangeClick, self)
    end)

    --Stars
    self.stars = {}
    for i = 1, Constant.BattleComboPointMax do
        local star = {}
        star.bg = self._ui["Panel/Panel_star/bg_"..i]
        star.star = self._ui["Panel/Panel_star/s_"..i]
        star.bg:setVisible(false)
        star.star:setVisible(false)
        self.stars[i] = star
    end

    self._ignoreRedPoint = false

	self:refresh()
end

function ClassRef:setIndex(idx)
    self._idx = idx
end
function ClassRef:getIndex()
    return self._idx
end

-- data = Skill
function ClassRef:refresh()
	if self._data then
		local data = self._data
		self:setNewTipVisibla( data:isNewGet() )
    	self:setName( data:getName() )
    	self:setIcon( data:getIcon() )

    	local costNum, costPath = data:getCostByClass()
		self:setManaCost( costNum )
		--self:setCostIcon( costPath )

		local costColor = data:getCostColorByClass()
		self:setManaCostColor( costColor )

		self:setLevel( data:getSkillLv() )

		self:setLock( not data:isLearned(), data:getMinLv() )

        self:setComboPointCost(data:getComboPoint())

        self:refreshRedPoint()
    end
end

function ClassRef:setIgnoreRedPoint(b)
    self._ignoreRedPoint = b
    if b then
        self._ui.tishi:setVisible(false)
    end
end

function ClassRef:refreshRedPoint()
    if self._ignoreRedPoint then return end

    -- 红点提示
    self._ui.tishi:setVisible(UD:getSkillPoint() > 0 and self._data:canUpgrade())
end

function ClassRef:setName(name)
	self._ui["Panel/name"]:setString(name)
end

function ClassRef:setEmptyIcon()
    self._ui["Panel/Panel_skill/icon"]:setVisible(false)
end

function ClassRef:setIcon(res)
	self._ui["Panel/Panel_skill/icon"]:loadTexture(res)
	self._ui["Panel/Panel_selected/icon"]:loadTexture(res)
end

function ClassRef:setManaCostNumVisible(b)
	self._ui["Panel/num"]:setVisible(b)
	self._ui["Panel/icon_num"]:setVisible(b)
end

-- 消耗图标
function ClassRef:setCostIcon( iconPath )
	if iconPath then
		self._ui["Panel/icon_num"]:loadTexture( iconPath, 1 )
	end
end

function ClassRef:setManaCost(n)
    if self._ui["Panel/num"] then
        local num=n or 0
	    self:setManaCostNumVisible(num > 0)
	    self._ui["Panel/num"]:setString(num)
    end
end

function ClassRef:setManaCostColor( colorTemp )
	self._ui["Panel/num"]:setTextColor( colorTemp or display.COLOR_WHITE )
end

function ClassRef:setLock(l,lv)
    self._ui["Panel/Panel_lock"]:setVisible(l)
    self._ui["Panel/Panel_lock/Text"]:setString(string.format(L("tips_3000180"),tostring(lv)))
    if l then
        self._ui["Panel/Button"]:setVisible(false)
        self._ui["Panel/Button"]:setOpacity(255)
        self._ui["Panel/Panel_skill"]:setOpacity(255*0.4)
        self._ui["Panel/Panel_skill/icon"]:setEnabled(false)
        --self._ui["Panel/icon_num"]:setOpacity(255*0.4)
        --self._ui["Panel/num"]:setOpacity(255*0.4)
        self._ui["Panel/Panel_selected"]:setOpacity(255*0.4)
        self._ui["Panel/Panel_selected/icon"]:setEnabled(false)
        self._ui["Panel/Panel_lock"]:setOpacity(255)
        self._ui["Panel/Panel_lv"]:setOpacity(255*0.4)
        self._ui["Panel/name"]:setOpacity(255*0.4)
    else
        self._ui["Panel/Button"]:setVisible(self._canChange)
        self._ui["Panel/Button"]:setOpacity(255)
        self._ui["Panel/Panel_skill"]:setOpacity(255)
        self._ui["Panel/icon_num"]:setOpacity(255)
        self._ui["Panel/num"]:setOpacity(255)
        self._ui["Panel/Panel_selected"]:setOpacity(255)
        self._ui["Panel/Panel_lock"]:setOpacity(255)
        self._ui["Panel/Panel_lv"]:setOpacity(255)
        self._ui["Panel/name"]:setOpacity(255)
        self._ui["Panel/Panel_skill/icon"]:setEnabled(true)
        self._ui["Panel/Panel_selected/icon"]:setEnabled(true)
    end
end

function ClassRef:setLevel(lv)
	self._ui["Panel/Panel_lv/lv_num"]:setString( "Lv.".. lv )
end

function ClassRef:setNewTipVisibla(b)
	-- self._ui.Icon_tips:setVisible(b)
	-- if b then
	-- 	display.playTimeline(self._ui.Icon_tips, nil, true)
	-- else
	-- 	display.pauseTimeline(self._ui.Icon_tips)
	-- end
end

function ClassRef:setChooseed(b)
	self._ui["Panel/Panel_skill/bg_choose"]:setVisible(b)
end

function ClassRef:setSelected(b)
	self._ui["Panel/Panel_skill"]:setVisible(not b)
	self._ui["Panel/Panel_selected"]:setVisible(b)
end

function ClassRef:unShowNameAndLv()
	self._ui["Panel/name"]:setVisible(false)
	self._ui["Panel/Panel_lv"]:setVisible(false)
end

function ClassRef:setClickEnable(b)
	self._ui.Panel:setTouchEnabled(b)
end

function ClassRef:setClickHandler(cb)
	self._onClick = cb
end

function ClassRef:setCanChange(b)
    self._canChange = b
    self._ui["Panel/Button"]:setVisible(b)
end
function ClassRef:isCanChange()
    return self._canChange
end

function ClassRef:setChangeHandler(cb)
	self._onChangeClick = cb
end

function ClassRef:setComboPointCost(point)
    self._ui["Panel/Panel_star"]:setVisible(point > 0)
    if point == 0 then return end

    local showStars = {}
    if point == 1 then
        showStars[1] = self.stars[3]
    elseif point == 2 then
        showStars[1] = self.stars[2]
        showStars[2] = self.stars[4]
    elseif point == 3 then
        showStars[1] = self.stars[2]
        showStars[2] = self.stars[3]
        showStars[3] = self.stars[4]
    else
        showStars = table.clone(self.stars)
    end

    for i,star in ipairs(self.stars) do
        star.bg:setVisible(false)
        star.star:setVisible(false)
    end

    for i = 1, #showStars do
        local bShine = point >= i
        local star = showStars[i]
        star.bg:setVisible(bShine)
        star.star:setVisible(bShine)
    end

end

return ClassRef
